package bTree.LeetCode;

import java.util.HashMap;
import java.util.Map;

public class 找到最大开销的子字符串 {
    public int maximumCostSubstring(String s, String chars, int[] vals) {
        int length = chars.length();
        Map<Character,Integer>map=new HashMap<>();
        for (int i = 0; i < length; i++) {
            map.put(chars.charAt(i), vals[i]);
        }
        int []dp=new int[s.length()];
        dp[0]=map.containsKey(s.charAt(0))?map.get(s.charAt(0)):s.charAt(0)-'a'+1;
        int maxNum=dp[0];
        for(int i=1;i<s.length();i++){
            int value=map.containsKey(s.charAt(i))?map.get(s.charAt(i)):s.charAt(i)-'a'+1;
            dp[i]=dp[i-1]<0?value:dp[i-1]+value;
            maxNum=Math.max(maxNum, dp[i]);
        }
        return Math.max(maxNum, 0);
    }
}